# encoding=utf-8
import sqlite3, sys

reload(sys)
sys.setdefaultencoding('utf8')

cx = sqlite3.connect("/sdcard/test.db")

cu = cx.cursor()

cu.execute("SELECT * FROM txm")
r = cu.fetchall()
print r


def to_str(i):
    if type(i) == 'Unicode':
        return i.encode('utf-8')
    if type(i) == 'str':
        return i
    return str(i)


if not r: exit()

r = '\n'.join(map(lambda x: ','.join(map(to_str, x)), r))
print r

f = open('/sdcard/test.csv', 'w')
f.write(r)
f.close()

cx.close()
